package com.jy.adscm.hcPlan.mapper.mysql;

import com.jy.adscm.hcPlan.bean.HcPlanMakePartExample;
import com.baomidou.mybatisplus.mapper.BaseMapper;
import com.jy.adscm.hcPlan.bean.support.HcPlanMakePartExampleSopport;
import com.jy.adscm.hcPlan.bean.support.HcPlanMakePartMergeSupport;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 单批次耗材零件制造配置 Mapper 接口
 * </p>
 *
 * @author yyy
 * @since 2018-08-15
 */
@Mapper
public interface HcPlanMakePartExampleMapper extends BaseMapper<HcPlanMakePartExample> {

    /**
     * 获取所有已经总装完成了的批次对应的零件制造配置
     * （总装结束时间大于系统时间）
     * @return
     */
    @Select("SELECT * FROM hc_plan_make_part_example HPMPE WHERE HPMPE.ASSEMBLY_ID IN ( SELECT id FROM hc_plan_final_assembly WHERE ass_end_date > SYSDATE())")
    public List<HcPlanMakePartExample> getAllAssemblyPartExamples();

    /**
     * 获取指定时间段内的零件制造配置
     * @return
     */
    @Select("SELECT * FROM hc_plan_make_part_example HPMPE WHERE " +
            "((make_start_time >= #{startTime} AND make_start_time <= #{endTime}) OR " +
            "(make_end_time >= #{startTime} AND make_end_time <= #{endTime})) " +
            "AND merge_id IS NOT NULL")
    public List<HcPlanMakePartExample> getAssemblyPartExamples(@Param("startTime") String startTime,@Param("endTime") String endTime);

    /**
     * 获取这个总装批次在这台机器上的最大的结束时间
     * @param make_machine
     * @param assembly_id
     * @return
     */
    @Select("SELECT MAX(make_end_time) FROM hc_plan_make_part_example WHERE make_machine = #{make_machine} AND assembly_id = #{assembly_id}")
    public Date getMaxEndDateThisAssemblyMachine(Integer make_machine,Long assembly_id);

    /**
     * 获取某一天的最大的并单序号
     * @param merge
     * @return
     */
    @Select("SELECT MAX(merge_sort_num) merge_sort_num FROM hc_plan_make_part_example WHERE merge_id LIKE '${merge}%'")
    public Integer getMaxMergeSortNum(@Param("merge") String merge);


    /**
     * 根据并单ID，获取并单详情
     * @param mergeId
     * @return
     */
    public HcPlanMakePartMergeSupport getMergePartDetail(String mergeId);


    @Select("SELECT MAX(hpme.make_end_time) make_end_time FROM hc_plan_make_part_example hpme WHERE hpme.make_machine = #{make_machine}")
    Date getMaxEndTime(@Param("make_machine")String make_machine);

//    @Select("SELECT MIN(hpme.make_start_time) make_end_time FROM hc_plan_make_part_example hpme WHERE hpme.make_machine = #{make_machine}")
    Date getMinStartTime(@Param("make_machine")String make_machine,@Param("assemblyIds") List<Long> assemblyIds);

    List<Long> getAllAssemblyIds(@Param("ids") List<Long> ids);

}
